package com.haoshuang.zhouzhoubang.ui.dialog

import android.content.Context
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.flyco.tablayout.listener.OnTabSelectListener
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.DialogChoosePlatformBenefitsBinding
import com.haoshuang.zhouzhoubang.databinding.ItemChoosePlatformBenefitsBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderShopBean
import com.haoshuang.zhouzhoubang.model.bean.WalletBean
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.core.CenterPopupView
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.imageResource
import com.mufeng.libs.utils.view.vertical
import com.mufeng.libs.utils.view.visibleOrGone
import com.mufeng.libs.utils.view.visibleOrInvisible
import kotlin.time.times

/**
 * 选择平台福利
 */
class ChoosePlatformBenefitsDialog(
    context: Context,
    private val welfare: OrderShopBean.WelfareBean?,
    private val onChoose: (welfare: OrderShopBean.WelfareItemBean?) -> Unit,
) : BottomPopupView(context) {

    private lateinit var binding: DialogChoosePlatformBenefitsBinding

    private val data = arrayListOf<OrderShopBean.WelfareItemBean>()
    private val adapter = ChoosePlatformBenefitsAdapter(data)

    private val titles = arrayListOf("可用福利", "不可用福利")

    private val useList = arrayListOf<OrderShopBean.WelfareItemBean>()
    private val notUseList = arrayListOf<OrderShopBean.WelfareItemBean>()


    override fun getImplLayoutId(): Int {
        return R.layout.dialog_choose_platform_benefits
    }

    override fun onCreate() {
        super.onCreate()
        binding = DialogChoosePlatformBenefitsBinding.bind(popupImplView)
        useList.clear()
        notUseList.clear()
        useList.add(OrderShopBean.WelfareItemBean(-1, "暂不使用", 0, true))
        useList.addAll((welfare?.can_use?.map {
            it.choosed = false
            it
        }?.toMutableList() ?: mutableListOf()))
        notUseList.addAll(welfare?.can_not_use ?: mutableListOf())
        binding.apply {

            slidingTabLayout.setTabData(titles.toTypedArray())
            slidingTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
                override fun onTabSelect(position: Int) {
                    if (position == 0){
                        data.clear()
                        data.addAll(useList)
                        adapter.canUse = true
                        adapter.notifyDataSetChanged()
                        tvConfirm.visibleOrInvisible(true)
                    }else{
                        data.clear()
                        data.addAll(notUseList)
                        adapter.canUse = false
                        adapter.notifyDataSetChanged()
                        tvConfirm.visibleOrInvisible(false)
                    }
                }

                override fun onTabReselect(position: Int) {

                }
            })
            data.clear()
            data.addAll(useList)
            recyclerView.vertical().adapter = adapter
            adapter.setOnItemClick { view, position, item ->
                if (adapter.canUse){
                    data.forEach {
                        it.choosed = it.id == item?.id
                    }
                    adapter.notifyDataSetChanged()
                }
            }

            tvConfirm.clickWithTrigger {
                // 确定
                val item = data.firstOrNull{it.choosed == true}
                if (item == null){
                    toast("请选择平台福利")
                    return@clickWithTrigger
                }
                dismiss()
                onChoose(item)
            }
        }
    }

    override fun getMaxHeight(): Int {
        return (ScreenUtils.getScreenHeight() * 0.3).toInt()
    }
}

class ChoosePlatformBenefitsAdapter(data: MutableList<OrderShopBean.WelfareItemBean>) :
    BaseBindingQuickAdapter<OrderShopBean.WelfareItemBean, ItemChoosePlatformBenefitsBinding>(data) {

    var canUse = true

    override fun onBindViewHolder(
        holder: BaseBindingHolder,
        position: Int,
        item: OrderShopBean.WelfareItemBean?
    ) {
        holder.getViewBinding<ItemChoosePlatformBenefitsBinding>().apply {
            ivImg.visibleOrGone(canUse)
            ivImg.imageResource = if (item?.choosed == true) R.mipmap.dz_ok else R.mipmap.dz_wx
            tvTitle.text =
                "${item?.welfare_name}${if (item?.id == -1) "" else "(剩余${item?.number})"}"
        }
    }

}